package service

import (
	"server/global"
	"server/plugin/amall/model"
	"server/plugin/amall/model/request"
	// "server/model/model"
	// request "server/model/model/request"
)

type BannerService struct {
}

// CreateBanner 创建轮播图记录
// Author [piexlmax](https://github.com/piexlmax)
func (bannerService *BannerService) CreateBanner(banner *model.Banner) (err error) {
	err = global.GVA_DB.Create(banner).Error
	return err
}

// DeleteBanner 删除轮播图记录
// Author [piexlmax](https://github.com/piexlmax)
func (bannerService *BannerService) DeleteBanner(ID string) (err error) {
	err = global.GVA_DB.Delete(&model.Banner{}, "id = ?", ID).Error
	return err
}

// DeleteBannerByIds 批量删除轮播图记录
// Author [piexlmax](https://github.com/piexlmax)
func (bannerService *BannerService) DeleteBannerByIds(IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]model.Banner{}, "id in ?", IDs).Error
	return err
}

// UpdateBanner 更新轮播图记录
// Author [piexlmax](https://github.com/piexlmax)
func (bannerService *BannerService) UpdateBanner(banner model.Banner) (err error) {
	err = global.GVA_DB.Model(&model.Banner{}).Where("id = ?", banner.ID).Updates(&banner).Error
	return err
}

// GetBanner 根据ID获取轮播图记录
// Author [piexlmax](https://github.com/piexlmax)
func (bannerService *BannerService) GetBanner(ID string) (banner model.Banner, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&banner).Error
	return
}

// GetBannerInfoList 分页获取轮播图记录
// Author [piexlmax](https://github.com/piexlmax)
func (bannerService *BannerService) GetBannerInfoList(info request.BannerSearch) (list []model.Banner, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.Banner{})
	var banners []model.Banner
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&banners).Error
	return banners, total, err
}
